import Component from './Component'

function getHandler(method){
    let handler=method[Component.handler];
    if (handler==null){
        handler=method[Component.handler]={verb:[],middleware:[]};
    }
    return handler;
}

function RequestMethod(verb){
    return function(target,name,desc){
        let handler=getHandler(target[name]);
        handler.target=target;
        handler.name=name;
        handler.verb.push(verb);
    }
}

export function config(opts){
    return function (target,name,desc){
        let handler=getHandler(target[name]);
        if (opts.middleware){
            if (opts.middleware instanceof Array){
                handler.middleware.push(...opts.middleware)
            }else{
                handler.middleware.push(opts.middleware)
            }
        }
        if (opts.path){
            handler.path=opts.path;
        }
    }
}

export function middleware(...middlewares){
    return config({middleware:middlewares});
}

export function path(path){
    return config({path});
}

export const get=RequestMethod("get");
export const post=RequestMethod("post");
export const put=RequestMethod("put");
export const del=RequestMethod("delete");
export const options=RequestMethod("options");
export const head=RequestMethod("head");
//export let trace=RequestMethod(Component,"trace");